﻿using EquipmentAlarmSystem.Models;
using EquipmentAlarmSystem.Service.Library;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EquipmentAlarmSystem.Controllers;

public class AuthorizationManagementController : Controller
{
    [Authorize]
    public IActionResult PermissionManagement([FromForm] Groupings permission)
    {
        if (permission.Id == null)
        {
            return View(Basics.GroupingData());
        }
        else
        {
            string req = Basics.GroupingDataWrite(permission);
            var val = Basics.GroupingData();
            val.Content = req;
            return View(Basics.GroupingData());
        }
    }

    [Authorize]
    public IActionResult AddGroup([FromForm] string name)
    {
        if (name is not null)
        {
            Basics.GroupingDataWrite(name);
        }
        return View(Basics.GroupingData());
    }

    [Authorize]
    public IActionResult EquipmentGrouping([FromForm] EquipmentGroupingSettingsModel groupingSettings)
    {
        EquipmentGroupingSettingsModel settingsModel = new();

        if (groupingSettings.ClientId is not null)
        {
            // 添加/删除数据
            settingsModel.Content = Basics.ModifyDevicesAndAddGroups(groupingSettings);
        }
        else
        {
            settingsModel.Content = "";
        }
        settingsModel.AuthorizationList = Basics.GetAuthorizedList();
        //获取所有客户端id
        settingsModel.ClientList = Basics.GetAllClientIds();
        settingsModel.GroupList = Basics.GetGroupName();

        return View(settingsModel);
    }

}
